function check_backup(instr)
% checks if data is backed up
% 
% checks location of all data associeated with stack IDs entered in the
% database (stacks.xls file). 
%
% function can be called without input (check all) or with
% instr: username (FMI) or animal ID


archive='\\argon.fmi.ch\gkeller.archive\RawData';

rig1Ana='\\KELLER-RIG1-ANA\RawData';
% rig12PI='\\KELLER-RIG1-2PI\RawData';

rig2Ana='\\KELLER-RIG2-ANA\RawData';
rig22PI='\\KELLER-RIG2-2PI\RawData';
rig2Aux='\\KELLER-RIG2-AUX\RawData';

tmp12PI='\\KELLER-RIG1-2PI\tempData2';
tmp1Aux='\\KELLER-RIG1-AUX\tempData';
tmp22PI='\\keller-rig2-2pi\tempData';
tmp2Aux='\\KELLER-RIG2-AUX\tempData';

cn_str = 'PROVIDER=SQLOLEDB; Data Source=PIA1; initial catalog=ExpLog; User ID=explog_user; password=Keller123';
DB = adodb_connect(cn_str, 240);
sql = 'SELECT Stacks.stackid, Stacks.expid, Stacks.comment, Experiments.siteid, Experiments.analysiscode, Sites.animalid, Sites.project, Animals.pi FROM Stacks INNER JOIN Experiments ON Stacks.expid=Experiments.expid INNER JOIN Sites ON Experiments.siteid=Sites.siteid INNER JOIN Animals ON Sites.AnimalID=Animals.AnimalID';
ExpLog = adodb_query(DB, sql);
DB.release;

ftypes={'bin' 'ini' 'lvd' 'eye' 'vid' 'vid2' 'run'};
ftype_value=[10 1 0.1 0.01 0.001 0.0001 0.00001];

paths_to_check_tmp={tmp12PI,tmp1Aux,tmp22PI,tmp2Aux};

paths_to_check={rig1Ana,rig22PI,rig2Aux,rig2Ana,archive};

stack_ids=cell2mat(ExpLog.stackid);
animal_ids=ExpLog.animalid;
user_ids=ExpLog.pi;

if nargin==0 % no input str
else
    if strcmp(instr(3),'_') %animal id is input str
        subset=strcmp(animal_ids,instr);
    else % user is input str
        subset=strcmp(user_ids,instr);
    end
    stack_ids=stack_ids(subset);
    animal_ids=animal_ids(subset);
    user_ids=user_ids(subset);
end




found_it=cell(length(stack_ids),length(paths_to_check_tmp)+length(paths_to_check));


% check the temp directories on the setup machines
for ind=1:length(paths_to_check_tmp)
    files=dir(paths_to_check_tmp{ind});
    files=files(3:end);
    
    curr_ids=[];
    curr_ftype={};
    cnt=0;
    for knd=1:length(files)
        if isempty(regexp(files(knd).name,'sec'))
            cnt=cnt+1;
            id_s=regexp(files(knd).name,'-')+2;
            id_e=regexp(files(knd).name,'_')-1;
            ftype_st=regexp(files(knd).name,'\.')+1;
            if  isempty(id_e)
                id_e=ftype_st-2;
            end
            if length(id_s)==1 && length(id_e)==1 && id_e>id_s
                curr_ids(cnt)=str2num(files(knd).name(id_s:id_e));
                curr_ftype{cnt}=files(knd).name(ftype_st:end);
            end
        end
    end
    
    for knd=1:length(stack_ids)
        matches=find(curr_ids==stack_ids(knd));
        if matches
%             curr_val=0;
%             for lnd=1:length(matches)
%                 curr_val=curr_val+ftype_value(strcmp(ftypes,curr_ftype{matches(lnd)}));
%             end
            found_it{knd,ind}=length(matches);
        end
    end
end


% check the mid-term and archive storage directories
for ind=1:length(paths_to_check)
    
    curr_ids=[];
    curr_ftype={};
    cnt=0;
    
    curr_users=unique(user_ids);
    for jnd=1:length(curr_users)
        curr_animals=unique(animal_ids);
        for lnd=1:length(curr_animals)
            curr_files=dir([paths_to_check{ind} '\' curr_users{jnd} '\' curr_animals{lnd} '\']);
            curr_files=curr_files(3:end);
            for knd=1:length(curr_files)
                if isempty(regexp(curr_files(knd).name,'sec'))
                    cnt=cnt+1;
                    id_s=regexp(curr_files(knd).name,'S.-')+4;
                    id_e=regexp(curr_files(knd).name,'_')-1;
                    ftype_st=regexp(curr_files(knd).name,'\.')+1;
                    if ~isempty(id_s)
                        if  isempty(id_e)
                            id_e=ftype_st-2;
                        end
                        curr_ids(cnt)=str2num(curr_files(knd).name(id_s:id_e));
                        curr_ftype{cnt}=curr_files(knd).name(ftype_st:end);
                    end
                end
            end
        end
    end

    for knd=1:length(stack_ids)
        matches=find(curr_ids==stack_ids(knd));
        if matches
%             curr_val=0;
%             for lnd=1:length(matches)
%                 curr_val=curr_val+ftype_value(strcmp(ftypes,curr_ftype{matches(lnd)}));
%             end
            found_it{knd,ind+length(paths_to_check_tmp)}=length(matches);
        end
    end
end

num_dirs=length(paths_to_check_tmp)+length(paths_to_check);
for knd=1:length(stack_ids)
    names{knd}=num2str(stack_ids(knd));
    if length(unique(cell2mat(found_it(knd,5:end))))==1
        found_it{knd,num_dirs+1}='';
    else
        found_it{knd,num_dirs+1}='X';
    end
    found_it{knd,num_dirs+2}=animal_ids{knd};
    found_it{knd,num_dirs+3}=user_ids{knd};
end

disp('____________________________________________________________________________________________________')
disp(sprintf(['\t|\t' 'Stack' '\t|\t' 't1I' '\t' 't1X' '\t' 't2I' '\t' 't2X' '\t|\t' 'r1A' '\t' 'r2I' '\t' 'r2X' '\t' 'r2A' '\t|\t' 'Arc' '\t|\t' 'con' '\t|\t' 'animal' '\t\t\t' 'user']))
disp('____________________________________________________________________________________________________')
for ind=1:size(found_it,1)
    disp(sprintf(['\t|\t' names{ind} '\t|\t' num2str(found_it{ind,1}) '\t' num2str(found_it{ind,2}) '\t' num2str(found_it{ind,3}) '\t' ...
        num2str(found_it{ind,4}) '\t|\t' num2str(found_it{ind,5}) '\t' num2str(found_it{ind,6}) '\t' ...
        num2str(found_it{ind,7}) '\t' num2str(found_it{ind,8}) '\t|\t' num2str(found_it{ind,9}) '\t|\t' found_it{ind,10} '\t|\t' ...
        found_it{ind,11} '  \t' found_it{ind,12}]))
end
disp('____________________________________________________________________________________________________')
disp(sprintf(['\t|\t' 'Stack' '\t|\t' 't1I' '\t' 't1X' '\t' 't2I' '\t' 't2X' '\t|\t' 'r1A' '\t' 'r2I' '\t' 'r2X' '\t' 'r2A' '\t|\t' 'Arc' '\t|\t' 'con' '\t|\t' 'animal' '\t\t\t' 'user']))

% dataset({found_it,'t1_I','t1_X','t2_I','t2_X','r1_I', 'r2_X', 'r1_A','r2_A','Arch','cons','animal','user'},'obsnames',names)

% fprintf('\r')
% 
% for ind=1:length(ftypes)
%     fprintf([ftypes{ind} ':' num2str(ftype_value(ind)) ' - '])
% end
% fprintf('\r')
















1